package gui;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

public class SwingUtils {

	
	/**
	 * 设置窗口弹出位置
	 * @param jFrame
	 * @param x
	 * @param y
	 */
	public static void set(JFrame jFrame,int x,int y){
		Dimension   screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
		jFrame.setLocation((int)screenSize.getWidth()/x,(int)screenSize.getHeight()/y);
	}
	/**
	 * 在Text空间里查找关键字，全部高亮显示,滚动到发现关键字的第一个位置
	 * @param text
	 * @param keyword
	 */
	private void findKeyWord(JTextArea text,String[] keyword){
		Highlighter lh = text.getHighlighter();
		lh.removeAllHighlights();
		String str = text.getText();
		try {
			int pos = 0;
			int start = 0;
		      for (int i = 0; i < keyword.length; i++) {
		          while ((pos = str.indexOf(keyword[i], pos)) >= 0) {
		        	  if(start==0)
		        		  text.setCaretPosition(pos);
		        	  start = pos;
		        	  //把关键字加入高亮组织
		            lh.addHighlight(pos, pos + keyword[i].length(),DefaultHighlighter.DefaultPainter);
		            pos += keyword[i].length();
		          }
		        }
		      if(start==0)
		    	  JOptionPane.showMessageDialog(text,"没有找到任何结果！");
			} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
